package com.gzz.boot.sequence;

public class IDTask implements Runnable{

    private IDService idService;

    private int counter;

    private Long start = 0L;

    public IDTask(IDService idService) {
        this.idService = idService;
        this.counter = 0;
    }

    public void run() {
        this.start = System.currentTimeMillis();
        String threadName = Thread.currentThread().getName();
        while (true) {
            try {
                Thread.sleep(1L);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }

            this.idService.getNextID();
            counter += 1;

            if (timeout()) {
                IDTaskManager.addCounter(threadName, counter);
                return;
            }
        }
    }


    public boolean timeout() {
        return (System.currentTimeMillis() - start) > 60*1000L;
    }
}
